package game.server;

import game.handler.JsonLoginMsgHandler;
import game.handler.PingMsgHandler;
import game.handler.ReqEnterSceneHandler;
import game.handler.RunMsgHandler;

import java.util.HashMap;
import java.util.Map;

import game.handler.IMessageHandler;
import game.handler.LoginMsgHandler;
import game.kcp.BaseKcpServer;
import game.kcp.KcpMesssageServerHandler;
import game.net.BaseTcpServer;
import game.net.ExternalTcpDecoder;
import game.net.ExternalTcpEncoder;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.MessageToByteEncoder;

public class GameServer extends BaseTcpServer {

    private final Map<Integer, IMessageHandler> handlerMap;

    public GameServer() {
        handlerMap = new HashMap<>();
        handlerMap.put(100, new LoginMsgHandler());
        handlerMap.put(102, new PingMsgHandler());
        handlerMap.put(1000, new JsonLoginMsgHandler());
        handlerMap.put(1001, new ReqEnterSceneHandler());
        handlerMap.put(1003, new RunMsgHandler());
    }

    //同时开启tcp和kcp连接
    public void start(int idleTime) throws Exception {
        //开启tcp 9811
        super.start(9811, idleTime);
        //开启kcp 9812
        KcpMesssageServerHandler kcpMsgHandler = new KcpMesssageServerHandler();
        kcpMsgHandler.handlerMap = handlerMap;
        BaseKcpServer server = new BaseKcpServer(9812, 1, kcpMsgHandler);
        server.start();
    }

    @Override
    public ChannelInboundHandlerAdapter getBusinessHandler() {
        return new ServerHandler();
    }

    @Override
    public ByteToMessageDecoder getTcpDecoder() {
        return new ExternalTcpDecoder(handlerMap);
    }

    @Override
    public MessageToByteEncoder<Object> getTcpEncoder() {
        return new ExternalTcpEncoder();
    }
}
